import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;

public class EnterIncome extends JFrame {

	/**
	 * 
	 */
	final static JFrame f = new JFrame();//Declaring the frame used for date picker
	private static final long serialVersionUID = 4660825795923830401L;
	private JPanel contentPane;
	public static JTextField tfDate,tfDescription,tfAmount;
	public static JComboBox cbCategory, cbReoccuring;
	public static DefaultListModel incomeListModel;
	public static JList incomeJList;
/*	private JTable incomeTable;
	private	JScrollPane scrollPane;*/
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EnterIncome frame = new EnterIncome();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private void btnDate_actionPerformed(ActionEvent e)
	{	//Calls date picker for end date for the reports
		tfDate.setText(new DatePicker(EnterIncome.f).setPickedDate());//Calls date picker
	}

	/**
	 * Create the frame.
	 */
	public EnterIncome() {
		
		onLoad();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 542, 410);
		this.setTitle("Enter Income");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblDate = new JLabel("Date:");
		lblDate.setBounds(10, 70, 70, 14);
		contentPane.add(lblDate);
		
		JButton btnDate = new JButton("Set");
		btnDate.setBounds(88, 68, 55, 20);
		contentPane.add(btnDate);
		btnDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				btnDate_actionPerformed(e);

			}
		});
		
		tfDate = new JTextField();
		tfDate.setBounds(150, 68, 106, 20);
		contentPane.add(tfDate);
		tfDate.setColumns(10);
		tfDate.setEditable(false);
		
		JLabel lblCategory = new JLabel("Category:");
		lblCategory.setBounds(309, 71, 70, 14);
		contentPane.add(lblCategory);
		
		cbCategory = new JComboBox(getCategoryItems());
		cbCategory.setBounds(396, 68, 106, 20);
		contentPane.add(cbCategory);
		
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setBounds(10, 100, 70, 14);
		contentPane.add(lblDescription);
		
		tfDescription = new JTextField();
		tfDescription.setBounds(150, 94, 106, 20);
		contentPane.add(tfDescription);
		tfDescription.setColumns(10);
		
		JLabel lblAmount = new JLabel("Amount:");
		lblAmount.setBounds(309, 97, 70, 14);
		contentPane.add(lblAmount);
		
		tfAmount = new JTextField();
		tfAmount.setBounds(396, 97, 104, 20);
		contentPane.add(tfAmount);
		tfAmount.setColumns(10);
		
		JLabel lblReoccuringEvery = new JLabel("Reoccuring:");
		lblReoccuringEvery.setBounds(309, 122, 123, 14);
		contentPane.add(lblReoccuringEvery);
		
		cbReoccuring = new JComboBox(getReoccuringItems());
		cbReoccuring.setBounds(396, 122, 106, 20);
		contentPane.add(cbReoccuring);
		
		JButton btnClear = new JButton("Clear");
		btnClear.setBounds(309, 146, 94, 23);
		contentPane.add(btnClear);
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Income.btnClear_actionPerformed(e);

			}
		});
		

		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(408, 146, 94, 23);
		contentPane.add(btnSubmit);
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Income.btnAddIncome_actionPerformed(e);
				
			}
		});
		
		JLabel lblIncomeList = new JLabel("Income List:");
		lblIncomeList.setBounds(10, 174, 123, 14);
		contentPane.add(lblIncomeList);
			//http://gilbertadjin.wordpress.com/2009/05/05/populating-a-jtable-with-a-collection-list/
		incomeListModel = new DefaultListModel();//Creates a list model
/*		String columnNames[] = {"Date", "Description", "Category", "Amount", "Reoccuring"};
		String dataValues[][] =
			{
				{"","","","","",""}
			};
		incomeTable = new JTable(dataValues,columnNames);
		scrollPane = new JScrollPane(incomeTable);*/
		
		
		
		incomeJList = new JList(incomeListModel);//Links the list model to the jlist
		JScrollPane jScrollPanel = new JScrollPane(incomeJList);//Adds a scroll panel for when too many items are added to jList
		incomeJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jScrollPanel.setBounds(10, 196, 492, 146);
		contentPane.add(jScrollPanel);
		
		for(int i = 0; i < Income.incomeList.size();i++)
		{
			incomeListModel.addElement( Income.incomeList.get(i));
		}
		
		JButton btnBackToMain = new JButton("Back to Main Menu");
		btnBackToMain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String filename = "IncomeList.ser";
				FileOutputStream fos = null;
				ObjectOutputStream out = null;
				try {
					fos = new FileOutputStream(filename);
					out = new ObjectOutputStream(fos);
					out.writeObject(Income.incomeList);//Writes the patient array list to the file
					out.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}		
				dispose();

				IncomeScreen incomePage = new IncomeScreen();
				incomePage.setVisible(true);
			}
		});
		btnBackToMain.setBounds(309, 345, 170, 23);
		contentPane.add(btnBackToMain);
		

	}
	
	public static String[] getReoccuringItems()
	{
		String[] procListItems = {"Nonreoccuring","Week","Month","Fortnight","Year"};

		
		return procListItems;
	}
	
	public static String[] getCategoryItems()
	{
		String[] procListItems = {"Wage","Shares","Interest","Dividends"};

		
		return procListItems;
	}
	
	@SuppressWarnings("unchecked")
	private void onLoad()
	{//Function for loading patients and procedures
		File filename = new File("IncomeList.ser");
		FileInputStream fis = null;
		ObjectInputStream in = null;
		if(filename.exists())
		{
			try {
				fis = new FileInputStream(filename);
				in = new ObjectInputStream(fis);
				Income.incomeList = (ArrayList<Income>) in.readObject();//Reads in the income list from the file
				in.close();
			} catch (Exception ex) {
				System.out.println("Exception during deserialization: " +
						ex); 
				ex.printStackTrace();
			}
		}
	}
	
	
}
